package com.huanbao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huanbao.base.dto.ExchangeDto;
import com.huanbao.base.dto.ShoppingSearchDto;
import com.huanbao.base.lang.Result;
import com.huanbao.entity.ShoppingMall;
import com.huanbao.service.ShoppingMallService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 积分商城 前端控制器
 * </p>
 *
 * @author zq
 * @since 2021-04-29
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShoppingMallController {

    private final ShoppingMallService shoppingMallService;


    @GetMapping("/list")
    public Result getList() {
        List<ShoppingMall> list = shoppingMallService.list(new QueryWrapper<ShoppingMall>().gt("number", 0));
        return Result.success(list);
    }

    /**
     * 用户兑换操作
     *
     * @param exchange 兑换相关信息
     * @return 200
     */
    @PostMapping("/exchange")
    @RequiresAuthentication
    public Result exchangeOne(@RequestBody @Validated ExchangeDto exchange) {
        return shoppingMallService.exchange(exchange);
    }

    /**
     * 通过类型找商品
     *
     * @param searchDto 查询参数
     * @return 200
     */
    @PostMapping("/search")
    public Result getListByType(@RequestBody ShoppingSearchDto searchDto) {
        QueryWrapper<ShoppingMall> wrapper = new QueryWrapper<ShoppingMall>().gt("number", 0).eq("deleted", 0);
        if (null != searchDto.getType() && !"".equals(searchDto.getType())) {
            wrapper.eq("type", searchDto.getType());
        }
        if (null != searchDto.getItemName() && !"".equals(searchDto.getItemName())) {
            wrapper.like("item_name", searchDto.getItemName());
        }
        List<ShoppingMall> list = shoppingMallService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 通过名称找商品
     *
     * @param itemName 物品名称
     * @return 200
     */
    @GetMapping("/listByType/{itemName}")
    public Result getListByItemName(@PathVariable("itemName") String itemName) {
        List<ShoppingMall> list = shoppingMallService.list(new QueryWrapper<ShoppingMall>().like("item_name", itemName).gt("number", 0));
        return Result.success(list);
    }

    @GetMapping("/typeList")
    public Result getTypeList() {
        List<ShoppingMall> list = shoppingMallService.list(new QueryWrapper<ShoppingMall>().gt("number", 0));
        ArrayList<String> result = new ArrayList<>();
        for (ShoppingMall shoppingMall : list) {
            if (!result.contains(shoppingMall.getType())) {
                result.add(shoppingMall.getType());
            }
        }
        return Result.success(result);
    }

    @PostMapping("/insert")
    public Result insertShopping(@RequestBody ShoppingMall shopping) {
        shoppingMallService.save(shopping);
        return Result.success(null);
    }

}
